package com.cgbl.cg.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.*
import com.cgbl.cg.databinding.AppFragmentHomeCourseBinding
import com.cgbl.cg.ui.WebActivity
import com.cgbl.cg.vm.home.HomeCourseViewModel
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.model.BannerBean
import com.robot.base.model.CourseBean
import com.robot.base.model.RibbonBean
import com.robot.base.model.TypeListBean
import com.robot.base.utils.pic.ImageLoaderUtils
import com.robot.base.view.DividerItemDecoration
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder

class HomeCourseFragment : GLBaseFragment<AppFragmentHomeCourseBinding, HomeCourseViewModel>() {
    private lateinit var bannerTitleAdapter: BannerImageAdapter<BannerBean>

    private lateinit var recommendCourseAdapter: RecommendCourseAdapter
    private lateinit var homeTabAdapter: HomeTabAdapter
    private lateinit var newCourseAdapter: NewCourseAdapter
    private lateinit var systemCourseAdapter: SystemCourseAdapter
    private lateinit var moreCourseAdapter: MoreCourseAdapter

    override fun initContentView(
        inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_home_course

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        viewModel.refresh()
        viewModel.fetchCourseClassificationData()
        initBanner()
        initAdapter()
    }

    override fun initData() {
        super.initData()
    }

    private fun initBanner() {
        bannerTitleAdapter = object : BannerImageAdapter<BannerBean>(
            arrayListOf()
        ) {
            override fun onBindView(
                holder: BannerImageHolder?,
                data: BannerBean?,
                position: Int,
                size: Int
            ) {
                    ImageLoaderUtils.getGlideManager()
                        .load(data?.imagesFile)
                        .apply(ImageLoaderUtils.getRoundCornerGlideOption(10))
                        .into(holder?.imageView!!)
            }
        }
        binding.banner.setAdapter(bannerTitleAdapter)
        binding.banner.setIndicator(binding.indicator, false)
        binding.banner.setBannerGalleryEffect(12, 4)
        binding.banner.start()

        bannerTitleAdapter.setOnBannerListener { data, position ->
            WebActivity.start(requireContext(), data.hrefUrl ?: "", data?.coverName ?: "")
        }
    }

    private fun initAdapter() {
        recommendCourseAdapter = RecommendCourseAdapter()
        binding.recyclerViewRecommend.apply {
            adapter = recommendCourseAdapter
        }

        homeTabAdapter = HomeTabAdapter()
        binding.recyclerViewTab.apply {
            adapter = homeTabAdapter
        }
        homeTabAdapter.setMTabChangeListener(object : HomeTabAdapter.TabChangeListener {
            override fun tabChange(position: Int, list: List<CourseBean>) {
                viewModel.position = position
                newCourseAdapter.setNewInstance(list as MutableList<CourseBean>)
            }
        })

        newCourseAdapter = NewCourseAdapter()
        binding.recyclerViewNewCourse.apply {
            adapter = newCourseAdapter
            addItemDecoration(
                DividerItemDecoration(
                    requireContext(), LinearLayoutManager.VERTICAL, 0.5f, R.color.app_F7F6FA
                )
            )
        }

        systemCourseAdapter = SystemCourseAdapter()
        binding.recyclerViewSystem.apply {
            adapter = systemCourseAdapter
        }

        moreCourseAdapter = MoreCourseAdapter()
        binding.recyclerViewMoreCourse.apply {
            adapter = moreCourseAdapter
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()

        viewModel.uc.ribbonDataEvent.observe(this) {
            bannerTitleAdapter.setDatas(it)
        }

        viewModel.uc.tabData.observe(this) {
            homeTabAdapter.setNewInstance(it as MutableList<TypeListBean>)
            if (it.isEmpty()) return@observe
            newCourseAdapter.setNewInstance(it[0].appCourseBaseVOS as MutableList<CourseBean>?)
        }
        viewModel.uc.hotData.observe(this) {
            recommendCourseAdapter.setNewInstance(it as MutableList<CourseBean>?)
        }
        viewModel.uc.systemData.observe(this) {
            systemCourseAdapter.setNewInstance(it as MutableList<CourseBean>?)
        }

        viewModel.uc.moreData.observe(this) {
            moreCourseAdapter.setNewInstance(it as MutableList<CourseBean>?)
        }

        viewModel.uc.refreshEvent.observe(this) {
            it ?: return@observe
            binding.smartRefreshLayout.finishRefresh()
            binding.smartRefreshLayout.finishLoadMore()
        }

//        binding.smartRefreshLayout.setOnLoadMoreListener {
//            it.finishLoadMore()
//            viewModel.toCourseMenuCommand.execute()
//        }

        binding.smartRefreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }
    }
}